function testCompletePack1(){
  let weight = [1, 3, 4] // 重量
  let value = [15, 20, 30] // 价值
  let bagWeight = 4  // 容量
  let dp = new Array(bagWeight+1).fill(0) // 初始化背包 dp[j]表示容量为j的背包能装的价值的多少
  // 递推公式 dp[j] = math.max(dp[j],dp[j-weight[i]]+value[i])
  // 因为取过后还可以再取值所以key从前向后遍历
  let len = weight.length
  for(let i = 0;i <= len;i++){
    // 先遍历物品 再遍历容量
    for(let j = 0;j <= bagWeight;j++){
      if(j >= weight[i]){
        dp[j] = Math.max(dp[j],dp[j-weight[i]]+value[i])
      }
    }
  }
  console.log(dp);
}
testCompletePack1()
// 先遍历物品，再遍历背包容量
function test_completePack1() {
  let weight = [1, 3, 4]
  let value = [15, 20, 30]
  let bagWeight = 4 
  let dp = new Array(bagWeight+1).fill(0)
  for(let i = 0;i <= weight.length;i++){
    for(let j = weight[i];j <= bagWeight;j++){
      // 也是从前向后
      dp[j] = Math.max(dp[j],dp[j-weight[i]]+value[i])
    }
  }
  console.log(dp);
}
test_completePack1()
function TestCompletePack(){
  let weight = [1, 3, 4]
  let value = [15, 20, 30]
  let bagWeight = 4
  let len = weight.length
  let dp = new Array(bagWeight+1).fill(0)
  for(let i = 0;i < len;i++){
    for(let j = bagWeight;j >= weight[i];j--){
      dp[j] = Math.max(dp[j],dp[j-weight[i]]+value[i])
    }
  }
  console.log(dp);
}
TestCompletePack()